/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ac.pbp.core.util;

import java.math.BigDecimal;
import java.util.Date;
import java.util.regex.Pattern;

/**
 *
 * @author Kim
 */
public class MyValidator {
    private static final Pattern datePattern = Pattern.compile("^[0-9]{2}$");
    private static final Pattern monthPattern = Pattern.compile("^[0-9]{2}$");
    private static final Pattern yearPattern = Pattern.compile("^[0-9]{4}$");
    private static final Pattern ddMMyyyyPattern = Pattern
                    .compile("^[0-9]{2}-[0-9]{2}-[0-9]{4}$");

    public static synchronized boolean validateDate(String date, String month, String year) {
        if ((date == null) || (month == null) || (year == null)
                        || date.equals("") || month.equals("") || year.equals("")) {
            return false;
        }

        return (MyValidator.datePattern.matcher(date).matches()
                && MyValidator.monthPattern.matcher(month).matches() && MyValidator.yearPattern
                .matcher(year).matches());
    }

    public static synchronized Integer getValidPageIndex(String page) {
        if ((page == null) || page.equals("")) {
                page = "1";
        }

        return Integer.valueOf(page);
    }

    public static synchronized boolean validateDate(String ddMMyyyy) {
        if ((ddMMyyyy == null) || ddMMyyyy.equals("")) {
            return false;
        }

        return (MyValidator.ddMMyyyyPattern.matcher(ddMMyyyy).matches());
    }

    public static synchronized boolean validateDate(Date date) {
        return (date != null);
    }

    public static synchronized boolean validateParam(String param) {
        return ((param != null) && !param.equals(""));
    }

    public static synchronized boolean validateIntParam(Integer param) {
        return (param != null);
    }

    public static synchronized boolean validateLongParam(Long param) {
        return (param != null);
    }

    public static synchronized boolean validateLongParamWithZeroPosibility(Long param) {
        return ((param != null) && !param.equals(Long.valueOf(0)));
    }

    public static synchronized boolean validateParamWithZeroPosibility(String param) {
        return ((param != null) && !param.equals("") && !param.equals("0"));
    }

    public static synchronized boolean validateParamWithZeroPosibility(Integer param) {
        return ((param != null) && !param.equals(Integer.valueOf(0)));
    }

    public static synchronized String getValidStringParam(String param) {
        return ((param != null) && !param.equals("")) ? param : null;
    }

    public static synchronized Integer getValidIntegerParam(String param) {
        return ((param != null) && !param.equals("")) ? Integer.valueOf(param)
                        : null;
    }

    public static synchronized Long getValidLongParam(String param) {
        return ((param != null) && !param.equals("")) ? Long.valueOf(param)
                        : null;
    }

    public static synchronized Integer getValidIntParamNonZero(String param) {
        return ((param != null) && !param.equals("") && !param.equals("0") ? Integer
                        .valueOf(param) : null);
    }

    public static synchronized String getValidStringParamNonZero(String param) {
        return ((param != null) && !param.equals("") && !param.equals("0") ? param
                        : null);
    }

    public static synchronized Long getValidLongParamNonZero(String param) {
        return ((param != null) && !param.equals("") && !param.equals("0") ? Long
                        .valueOf(param) : null);
    }

    public static synchronized Integer getValidIntegerParamOrOne(String param) {
        return ((param != null) && !param.equals("")) ? Integer.valueOf(param)
                        : Integer.valueOf(1);
    }

    public static synchronized Integer getValidIntegerParamOrZero(String param) {
        return ((param != null) && !param.equals("")) ? Integer.valueOf(param)
                        : Integer.valueOf(0);
    }

    public static final synchronized BigDecimal getBigDecimal(BigDecimal bigDecimal) {
        if (bigDecimal == null) {
            return BigDecimal.valueOf(0);
        }

        return bigDecimal;
    }

    public static final boolean isValidArrayParam(String[] param1, String[] param2) {
        return ((param1 != null) && (param1.length > 0) && (param2 != null)
                        && (param2.length > 0) && (param1.length == param2.length));
    }
}
